<?php

namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\History;
use App\Models\Platform;
use Illuminate\Http\Request;

class ChannelController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @param string $category
     * @param Category $categories
     * @param Platform $platforms
     * @return \Illuminate\Http\Response
     */
    public function index($category = '', Category $categories, Platform $platforms)
    {
        $categories = $categories::all();
        $platforms = $platforms->all()->map(function (Platform $platform) use ($category) {
            return $platform->setCategory($category)->append('rooms');
        });
        $category = $categories->where('url', '/channel/' . $category)->first();
        return view('channel', compact(
            'categories',
            'category',
            'platforms'
        ));
    }

    /**
     * @author kuddy
     * @param $key
     * @param History $history
     * @param \Auth $auth
     * @param \Cache $cache
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function jump($key, History $history, \Auth $auth, \Cache $cache)
    {
        if (!$cache::has($key)) {
            abort(404);
        }
        $room_info = $cache::get($key);
        if ($auth::check() && !$history->updateHistory($room_info)) {
            abort(500);
        };
        return redirect($room_info['url']);
    }
}
